package nullby.common.utils;

import java.util.Set;


import android.content.Context;
import android.content.SharedPreferences;

//TODO: add prefix
public abstract class PreferencesManager {

	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;

	public abstract String getTag();

	public abstract String getPreferencesName();

	public abstract Context getPreferencesContext();

	public PreferencesManager() {
		preferences = getPreferencesContext().getSharedPreferences(getPreferencesName(),
				Context.MODE_PRIVATE);
		editor = preferences.edit();
		CustomLog.logD(getTag(), "create Preferences Manager");
	}

	public PreferencesManager(Context context, final String name) {
		preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
		editor = preferences.edit();
		CustomLog.logD(getTag(), "create Preferences Manager");
	}

	public void commit() {
		getEditor().commit();
		CustomLog.logD(getTag(), "commit preferences");
	}

	public void clear() {
		getEditor().clear();
		CustomLog.logD(getTag(), "clear preferences");
		commit();
	}

	public void remove(final String key) {
		getEditor().remove(key);
		CustomLog.logD(getTag(), "remove " + key + " preferences");
		commit();
	}

	public SharedPreferences.Editor getEditor() {
		if (editor == null) {
			editor = preferences.edit();
		}
		return editor;
	}

	public void putString(final String key, final String value) {
		getEditor().putString(key, value);
		CustomLog.logD(getTag(), "put string preferences");
		commit();
	}

	public void putBoolean(final String key, final Boolean value) {
		getEditor().putBoolean(key, value);
		CustomLog.logD(getTag(), "put boolean preferences");
		commit();
	}

	public void putFloat(final String key, final Float value) {
		getEditor().putFloat(key, value);
		CustomLog.logD(getTag(), "put float preferences");
		commit();
	}

	public void putInt(final String key, final Integer value) {
		getEditor().putInt(key, value);
		CustomLog.logD(getTag(), "put int preferences");
		commit();
	}

	public void putLong(final String key, final Long value) {
		getEditor().putLong(key, value);
		CustomLog.logD(getTag(), "put long preferences");
		commit();
	}

	public void putStringSet(final String key, final Set<String> value) {
		getEditor().putStringSet(key, value);
		CustomLog.logD(getTag(), "put stringSet preferences");
		commit();
	}

	public String getString(final String key) {
		try {
			return preferences.getString(key, "");
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get string error", e);
		}
		return null;
	}

	public Boolean getBoolean(final String key) {
		try {
			return preferences.getBoolean(key, false);
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get boolean error", e);
		}
		return null;
	}

	public Float getFloat(final String key) {
		try {
			return preferences.getFloat(key, 0F);
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get float error", e);
		}
		return null;
	}

	public Integer getInt(final String key) {
		try {
			return preferences.getInt(key, 0);
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get integer error", e);
		}
		return null;
	}

	public Long getLong(final String key) {
		try {
			return preferences.getLong(key, 0L);
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get long error", e);
		}
		return null;
	}

	// TODO: Set<String> initialize
	public Set<String> getStringSet(final String key) {
		try {
			return preferences.getStringSet(key, null);
		} catch (Exception e) {
			CustomLog.logE(getTag(), "get stringSet error", e);
		}
		return null;
	}

}
